package com.songlanyun.modules.recommendassist.controller.admin;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import com.songlanyun.common.annotation.AuthIgnore;
import com.songlanyun.common.exception.RRException;
import com.songlanyun.common.utils.DateUtils;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.R;
import com.songlanyun.modules.exception.DonationExceptionEnum;
import com.songlanyun.modules.recommendassist.entity.RecommendAssistEntity;
import com.songlanyun.modules.recommendassist.model.vo.RecommendAssistVO;
import com.songlanyun.modules.recommendassist.service.RecommendAssistService;
import com.songlanyun.modules.user.controller.AbstractController;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.activation.MimetypesFileTypeMap;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 推荐帮扶
 *
 * @author wuchao
 * @email songlanyun@gmail.com
 * @date 2022-08-06 09:24:43
 */
@RestController
@RequestMapping("/admin/recommend/assist")
public class RecommendAssistAdminController extends AbstractController {
    @Autowired
    private RecommendAssistService recommendAssistService;

    /**
     * 列表
     */
    @GetMapping("/page")
    public R page(@RequestParam Map<String, Object> params) {
        PageUtils page = recommendAssistService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        RecommendAssistEntity recommendAssist = recommendAssistService.getById(id);

        return R.ok().put("data", recommendAssist);
    }


    /**
     * 联系
     */
    @PostMapping("/contact")
    public R contact(@RequestBody RecommendAssistEntity recommendAssist) {
        recommendAssistService.contact(recommendAssist);

        return R.ok();
    }

    /**
     * 导出
     */
    @GetMapping("/export")
    @AuthIgnore
    public void export(@RequestParam Map<String, Object> params, HttpServletResponse response) throws IOException {
        try {
            //校验token
            this.getAdminByToken(response);
        } catch (RRException e) {
            return;
        }

        List<RecommendAssistVO> vos = recommendAssistService.selectVosList(params);
        export(response, RecommendAssistVO.class, vos, "推荐帮扶列表");
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable("id") Long id) {
        recommendAssistService.removeById(id);

        return R.ok();
    }

    public static void main(String[] args) {
        List<RecommendAssistVO> vo = new ArrayList<>();
        RecommendAssistVO recommendAssistVO = new RecommendAssistVO();
        vo.add(recommendAssistVO);

        Type clazz = vo.getClass().getGenericSuperclass();
        System.out.println(clazz.getClass());
        ParameterizedType pt = (ParameterizedType) clazz;
        System.out.println(pt.getActualTypeArguments()[0].getTypeName());
        System.out.println(pt.getActualTypeArguments()[0].toString());
    }

}
